package com.kds.smarthome.common;

import com.fasterxml.jackson.annotation.JsonValue;
import org.apache.ibatis.type.MappedTypes;

/**
 * 能源类型枚举
 */
@MappedTypes(EnergyType.class)
public enum EnergyType {
    ele("ele", "电"),
    water("water", "水"),
    gas("gas", "燃气");

    private final String code;
    private final String desc;

    EnergyType(String code, String desc) {
        this.code = code;
        this.desc = desc;
    }

    @JsonValue  // 确保JSON序列化时使用code值
    public String getCode() {
        return code;
    }

    public String getDesc() {
        return desc;
    }

    // 根据code获取枚举值
    public static EnergyType fromCode(String code) {
        for (EnergyType type : values()) {
            if (type.code.equals(code)) {
                return type;
            }
        }
        throw new IllegalArgumentException("Unknown EnergyType code: " + code);
    }
}
